proc mode {nick chan botnet host} {
  global botnick home bcast 

  if {![botisop $chan]} {
    putquick "NOTICE $nick :\0034ERROR - Bot dosen't have op status (+o) in $chan"
    putquick "PRIVMSG $home :Mode change request from \002$nick\002 ($host) in $chan failed - I'm not op'd."
    return 0
  }	
  
  # Determine channel modes that are valid and should be processed
  set validModes "hklmnopstv"

  # Determine channel modes that accept no parameters and do not support parameters
  set noParamModes "mnps"

  # Currently assume the mode is an invalid mode [0/1 bool value, 0=false]
  set currentlyValid 0

  # Currently assume the mode has parameters
  set currentlyNoParam 0

  # NEVER process modes with more than 1 parameter.  
  #  No supported mode interpreted by this proc requires more than 1 parameter

  # if user specified cannel mode with a +/- in front
  if {[string range $botnet 0 0] == "+" || [string range $botnet 0 0] == "-"} {
    # catch unsupported modes
  
    # Loop through each character in the valid modes string and compare it to the 
    #   letter denoting the mode the user is attempting to set.  If the user is attempting
    #   to set a valid channel mode, flag the mode as valid (1) otherwise leave it to its default
    #   value of invalid (0)
    for { set counter 0 } { $counter < [string length $validModes] } { incr counter } {
      if {[string range $validModes $counter $counter] == [string range $botnet 1 1]} {
        set currentlyValid 1
      }
    }    

    # Loop through each character in the no parameters string and compare it to the
    #   letter denoting the mode the user is attempting to set.  If a user is attempting 
    #   to set a mode involving parameters, flag the mode as not having parameters (1) otherwise
    #   leave it as being its default value of having parameters (0)
    for { set counter 0 } { $counter < [string length $noParamModes] } { incr counter } {
      if {[string range $noParamModes $counter $counter] == [string range $botnet 1 1]} {
        set currentlyNoParam 1
      }
    }  

    # catch incorrect mode assignments
    if {[string tolower $botnet] == "+k"} {
      putquick "NOTICE $nick :\0034ERROR - Should Be -\002 $botnick mode +k <set key>" 
      return 0
    } elseif {[string tolower $botnet] == "+l"} {
      putquick "NOTICE $nick :\0034ERROR - Should Be -\002 $botnick mode +l <whatever limit you want>" 
      return 0    
    } elseif {[string range [string tolower $botnet] 0 1] == "+o"} {
      putquick "NOTICE $nick :\0034ERROR - Should Be -\002 $botnick op <user to op>" 
      return 0    
    } elseif {[string range [string tolower $botnet] 0 1] == "+v" } {
      putquick "NOTICE $nick :\0034ERROR - Should Be -\002 $botnick voice <user to voice>" 
      return 0    
    } elseif {[string range [string tolower $botnet] 0 1] == "-o"} {
      putquick "NOTICE $nick :\0034ERROR - Should Be -\002 $botnick deop <user to de-op>" 
      return 0    
    } elseif {[string range [string tolower $botnet] 0 1] == "-v"} {
      putquick "NOTICE $nick :\0034ERROR - Should Be -\002 $botnick devoice <user to de-voice>" 
      return 0    
    } elseif {[string range [string tolower $botnet] 0 1] == "-h" || [string range [string tolower $botnet] 0 1] == "+h"} {
      # DO NOT SUPPORT +/-h
      # --------------------
      #  it takes the second parameter and changes modes matching the letters in the second
      #  parameter on servers where +/-h are not supported
      ########################################################################################
      #  This is a bug in eggdrop, not the TCL however the bug is not fixed in OTBN.tcl 2
 
      putquick "NOTICE $nick :\0034ERROR - Half-ops are not supported." 
      return 0    
    }

    #mode +t and -k are now supported without parameters as of X-Bot 2003 SP-1
  
  } else {
    putquick "NOTICE $nick :\0034ERROR - You must use a + in front of a mode to add a mode.  Use - to remove a mode"
    return 0
  }

  # Ensure setting a valid mode before proceeding.  If not a valid mode, display an error message
  if { $currentlyValid == 0 } {
    putquick "NOTICE $nick :\0034ERROR - The mode you are attempting to set is not yet supported." 
    return 0    
  }    

  # If the mode that is being set has no parameters, do not process any parameters
  if { $currentlyNoParam == 1 } {
    set botnet [string range $botnet 0 1]

    putlog "!$nick ($host)! mode $chan $botnet"
    putquick "MODE $chan $botnet"
    putquick "NOTICE $nick :\0033Sucessfully set mode \00312$botnet\0033 on\00312 $chan"  
  } else {
    # Process only 1 parameter (note this command takes the mode change and 1 parameter and assigns it to $botnet)
    set botnet "[join [lrange [split $botnet] 0 1]]"

    putlog "!$nick ($host)! mode $chan $botnet"
    putquick "MODE $chan $botnet"
    putquick "NOTICE $nick :\0033Sucessfully set mode \00312$botnet\0033 on\00312 $chan"
  }

  if {$bcast == "1"} {
    putquick "PRIVMSG $home :\002$nick\002 ($host) requested to me to mode \002$botnet\002 on \002$chan\002"
  }
}










